<?php
class XcateModel extends Model {
	protected	$tableName	=	'x_cate';
	protected	$fields		=	array (0 =>'cid',1 =>'pid',2 =>'list',3 =>'title',4 =>'info',5 =>'corder',6 =>'porder','_autoinc' =>false);

	/*
		需求举例：
		1，获取ask列的所有分类树 [ok:getTree]
		2，获取ask列执行路径子分类的信息 [ok:getTree]
		3，获取ask列中一个父分类的所有子分类 如 3 的子分类 是 4/5/6 [ok:getSon]
		4，获取ask列中一个分类所在的支干 如 3 在支干 1,2,3 中 [ok:getAllTree]
	*/

//	//获取分类树 TODO 列表深度
//	public function getTree($list,$pid='0',$deep=0) {
//		//过滤列表值
//		$list	=	$this->_strip_key($list);
//		//获取分类Tree
//		$data	=	$this->_makeTree($list,$pid);
//		return $data;
//	}
	
	//按路径获取Tree信息
	public function getTree($list,$path=false){
		//过滤list值
		$list	=	$this->_strip_key($list);
		//获取完整的Tree
		$tree	=	$this->_getData($list);
		if(!$path) return $tree;
		
		//输出完整Tree
		
		//按路径进行查找
		$paths	=	explode(',',$path);
		//路径深度
		$deep	=	count($paths);
		//格式化成索引格式
		$tree	=	$this->_formatTree($tree);
		
		//dump($tree);
		//查找
		foreach($paths as $v){
			if($deep>1){
				$tree	=	$tree[$v][d];
			}else{
				$tree	=	$tree[$v];
			}
			$deep--;
		}
		$new_tree[]	=	$tree;
		//格式化成正常格式
		$new_tree	=	$this->_formatTree($new_tree,true);
		return $new_tree;
	}

	//获取所有tree的子节点集合
	public function getSon($list,$path=false) {
		//获取Tree
		$tree	=	$this->getTree($list,$path);
		if(count($tree)==0) return false;
		$newtree	=	array();
		$this->_getSon($tree,$newtree);
		return $newtree;
	}
	
	/**
	 * 获得所有可能的分类组合
	 *
	 */
	public function getAllTree($list,$path){
		//过滤list值
		$list	=	$this->_strip_key($list);
		//获取完整的Tree
		$tree	=	$this->_getData($list);
		$tree = $this->_formatTree($tree);
		//$treeTagId = $this->_parseTree($tree);
		$result = $this->getMayBeTree($tree,$path);

		return $result;
	}
	
	//保存分类树
	public function saveTree($key='category:ask',$tree=array()) {
		$tree	=	$this->_parseTree($tree);
		$xdata	=	model('Xdata')->put($key,$tree,true);
//		$tree	=	model('Xdata')->get($key);
//		F($list.'_cate_tree',$tree);
		if(!$xdata){
			return false;
		}else{
			return $tree;
		}
	}
	
	private function getMayBeTree($tree,$path){
		$result = array();
		foreach($tree as $key=>$value){
			if($key == $path){
				if(is_array($value['d'])){
					foreach($value['d'] as $k=>$val){
							$result[] = $key.','.$k;
					}
				}
			}else{
				if(is_array($value['d'])){
					foreach($value['d'] as $k=>$val){
						if($k == $path){
							$result[] = $key.','.$k;
						}
					}
				}
			}
		}
		return $result;
	}
	
	/**
	 *	将树转化成左右值的树 
	 *
	 */
	public function getLeftAndRight($tree,$left,$id,&$result=array(),$name='root'){
		$right = $left + 1;
		if(!empty($tree)){
			foreach($tree as $key=>$value){
				//if(!empty($value['d'])){
					$right = $this->getLeftAndRight($value['d'],$right,$value['a'],$result,$value['t']);	
				//}
			}
		}
		if(!isset($result[$id])){
			$result[$id]['left'] = $left;
			$result[$id]['right'] = $right;
			$result[$id]['name']  = $name;
		}
		return $right+1;
	}
	
	public function getParentLR($tree,$id){
		$result = array();
		foreach($tree as $key=>$value){
			if($value['left']<$tree[$id]['left'] && $value['right']>$tree[$id]['right']){
				$result[] = $key;
			}
		}
		$result = array_reverse($result);
		array_shift($result);
		//$result[] = $id;
		
		return implode(',',$result);
	}
	
	//解析分类树，将其中的tagid解析出来
	public function _parseTree($tree=array()){
		$xtag	=	model('Xtag');

		foreach($tree as $k=>$v){
			$tree[$k][o]	=	$k;
			$tree[$k][a]	=	$xtag->getTagId($v[t]);
			asort($tree[$k]);
			if(is_array($v[d])){
				$tree[$k][d]	=	$this->_parseTree($v[d]);
			}
		}

		return $tree;
	}

	public function _getSon($tree,&$new_cate=array()) {
		foreach($tree as $v){
			$new_cate[]	=	$v[a];
			if(is_array($v[d])){
				$this->_getSon($v[d],$new_cate);
			}
		}
	}
	//将Tree转换成索引格式,第二个参数是逆向格式化 即格式化成正常JS可识别的格式
	public function _formatTree($tree,$r=false){

		$new_cate	=	array();
		$k = -1;
		foreach($tree as $v){
			
			if(!$r){		
				$k = $v[a]; 
			}else{
				$k ++;
			}
			$new_cate[$k][a]	=	$v[a];
			$new_cate[$k][t]	=	$v[t];
			$new_cate[$k][e]	=	$v[e];
			$new_cate[$k][m]	=	$v[m];
			
			
			if(is_array($v[d])){
				$new_cate[$k][d]	=	$this->_formatTree($v[d],$r);
			}
		}
		return $new_cate;
	}
	
	//获取一个树
	public function _getData($list='ask'){
//		$tree	=	F($list.'_cate_tree');
//		if(!$tree){
//			//$tree	=	$this->_makeTree($list);
			$tree	=	model('Xdata')->get('category:'.$list);
			F($list.'_cate_tree',$tree);
//		}
		return $tree;
	}

//	//生成分类Tree结构
//	protected function _makeTree($list,$pid) {
//		$c = $this->where("`list`='$list' AND pid='$pid'")->findAll();
//		if( !empty($c) ){
//			foreach($c as $v){
//				$cTree['t']	=	$v['title'];
//				$cTree['a']	=	$v['cid'];
//				$cTree['d']	=	$this->_makeTree($list,$v['cid']);
//				$cTrees[]	=	$cTree;
//			}
//		}
//		
//		return $cTrees;
//	}
	
//	/* 非交叉分类 */
//	//解析分类
//	public function _digCate($array,$digstring='cate') {
//		foreach($array as $k=>$v){
//			$nk	=	str_replace($digstring,'',$k);
//			if(is_numeric($nk) && !empty($v)){
//				$cates[$nk]	=	intval($v);
//			}
//		}
//		$pid	=	is_array($cates)?end($cates):0;
//		unset($cates);
//		return intval($pid);
//	}
//
//	//解析分类树
//	public function _digCateTree($array,$digstring='cate') {
//		foreach($array as $k=>$v){
//			$nk	=	str_replace($digstring,'',$k);
//			if(is_numeric($nk) && !empty($v)){
//				$cates[$nk]	=	intval($v);
//			}
//		}
//		if(is_array($cates)){
//			return implode(',',$cates);
//		}else{
//			return intval($cates);
//		}
//	}
//
//	//添加分类
//	public function addCate($list='',$cid='',$pid='',$parseId=false) {
//		//过滤列表值
//		$list	=	$this->_strip_key($list);
//		if(!$list) return false;
//		
//		$map['list']	=	$list;
//		
//		$xtag	=	model('Xtag');
//		if($parseId){		
//			$map['title']	=	t($cid);
//			$map['cid']		=	$xtag->getTagId($cid);
//			$map['pid']		=	$xtag->getTagId($pid);
//		}else{
//			$map['title']	=	$xtag->getTagName($cid);
//			$map['cid']		=	intval($cid);
//			$map['pid']		=	intval($pid);
//		}
//		//获取分类Tree
//		$data	=	$this->add($map);
//		return $data;
//	}
	
	//过滤key,允许格式 数字字母下划线，list:key 不允许出现html代码 和这些符号 ' " & * % ^ $ ? ->
	protected function _strip_key($key=''){

		$key	=	strip_tags($key);
		$key	=	str_replace(array('\'','"','&','*','%','^','$','?','->'),'',$key);

		return $key;
	}		
}
?>